import Base64 from "../libs/js-base64/index.js"
/*
 * 判断是否是window
 */
export const isWindow = (obj) => {
	return obj != null && typeof(window) !== 'undefined' && obj === window;
}
const class2type = {};
'Boolean,Number,String,Function,Array,Date,RegExp,Object'.split(',').forEach(value => {
	class2type['[object ' + value + ']'] = value.toLowerCase();
});
/*
 * 判断是否object类型
 */
export const isPlainObject = (obj) => {
	if (!obj || type(obj) !== 'object' || obj.nodeType || isWindow(obj)) {
		return false;
	};
	let hasOwn = Object.prototype.hasOwnProperty;
	try {
		if (obj.constructor && !hasOwn.call(obj, 'constructor') && !hasOwn.call(obj.constructor.prototype,
				'isPrototypeOf')) {
			return false;
		};
	} catch (e) {
		return false;
	};
	for (var key in obj) {};
	return key === undefined || hasOwn.call(obj, key);
};

/*
 * 判断是否array类型
 */
export const isArray = (obj) => {
	return Array.isArray(obj);
};

let type = function(obj) {
	return obj == null ? String(obj) : class2type[Object.prototype.toString.call(obj)] || 'object';
};

/*
 * 判断是否字符串
 */
export const isString = (obj) => {
	return type(obj) === 'string';
};
/*
 * Json转String
 */
export const JsonToString = (obj) => {
	return (isPlainObject(obj) || isArray(obj)) ? JSON.stringify(obj) : (obj + '');
};

/*
 * String转Json
 */
export const StringToJson = (obj) => {
	if (isString(obj)) {
		try {
			return JSON.parse(obj);
		} catch (e) {
			return obj;
		};
	} else {
		return obj;
	};
};

// 数据换行
export const showBRContent = (val) => {
	if (!val) {
		return val
	}
	return val.replace(/<br\s*\/?>/g, "\n")
}
// base64解码
export const showDecodeBase64 = (val) => {
	if (!val) {
		return;
	}
	try {
		return Base64.decode(val)
	} catch (e) {
		//TODO handle the exception
		console.log(e)
		return val;
	}
}

export const encodeBase64=(val)=>{
	if(val){
		return Base64.encode(val)
	}
	return val;
}